import java.awt.geom.Line2D;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: HP
 * Date: 2023 -04 -11
 * Time: 16:25
 */
public class Line {
    private MyPoint2D p1;
    private MyPoint2D p2;

    public Line(MyPoint2D p1, MyPoint2D p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public boolean intersectionLine(Line a) {
        double x1 = p1.getX();
        double y1 = p1.getY();
        double x2 = p2.getX();
        double y2 = p2.getY();
        double x3 = a.p1.getX();
        double y3 = a.p1.getY();
        double x4 = a.p2.getX();
        double y4 = a.p2.getY();

        if (Math.max(x1, x2) < Math.min(x3, x4) || Math.max(x3, x4) < Math.min(x1, x2)
                || Math.max(y1, y2) < Math.min(y3, y4) || Math.max(y3, y4) < Math.min(y1, y2)) {
            return false;
        }

        double u = Multiply(x3 - x1, y3 - y1, x2 - x1, y2 - y1);
        double v = Multiply(x4 - x1, y4 - y1, x2 - x1, y2 - y1);
        double w = Multiply(x1 - x3, y1 - y3, x4 - x3, y4 - y3);
        double z = Multiply(x2 - x3, y2 - y3, x4 - x3, y4 - y3);
        return (u * v <= 0.000001 && w * z <= 0.000001);
    }

    public boolean contains(MyPoint2D a) {
        return (Multiply(a.getX() - p1.getX(), a.getY() - p1.getY(), p2.getX() - p1.getX(), p2.getY() - p1.getY()) == 0
                && (a.getX() >= Math.min(p1.getX(), p2.getX())) && (a.getX() <= Math.max(p1.getX(), p2.getX()))
                && (a.getY() >= Math.min(p1.getY(), p2.getY())) && (a.getY() <= Math.max(p1.getY(), p2.getY())));
    }

    public static double Multiply(double px0,double py0,double px1,double py1){
        return px0*py1-px1*py0;
    }
}

